#ifndef TASKTREE_H
#define TASKTREE_H

#include "taskinfo.h"

class Filter
{
public:

    virtual bool filt(const pid_t &pid) = 0;

    virtual ~Filter() {}
};

enum class SortOrder
{
    ASC, DESC
};

class Location
{
public:

    Location(pid_t pid):m_pid(pid) {}

    Location(pid_t pid, pid_t ppid, int index):m_pid(pid),m_ppid(ppid),m_index(index) {}

    pid_t getPid() const { return m_pid; }

    pid_t getPPid() const { return m_ppid; }

    int getIndex() const { return m_index; }

    void setPPid(pid_t ppid) { m_ppid = ppid; }

    void setIndex(int index) { m_index = index; }

private:
    pid_t m_pid;
    pid_t m_ppid = 0;
    int m_index = -1;
};

class TaskTree_Private;

/* 进程树
每次使用loadFromSys都会根据系统实际情况重新构造一颗进程树
进程树中保存除0号进程外系统中的所有进程和他们之间的父子关系
树根为0号进程的直接子进程，可能不止一个
由于0号进程的特殊性，0号进程不保存在进程树中

进程树对象构造后，必须在需要查询进程树前调用flush，
构造函数不会从系统载入任何信息*/
class TaskTree
{
public:
    TaskTree();

    ~TaskTree();

    void addFilter(Filter * filter);

    void flush();

    void sort(TIP prop, SortOrder order);

    bool setTreeMode(bool treeMode);

    bool isTreeMode() const;

    SortOrder getSortOrder() const;

    SortBy getSortBy() const;

    int getRootCount() const;

    pid_t getRootAt(int n) const;

    int getTaskCount() const;

    bool contains(pid_t pid) const;

    int getChildCount(pid_t ppid) const;

    pid_t getChildAt(pid_t ppid, int n) const;

    bool hasParent(pid_t pid) const;

    pid_t getParent(pid_t pid) const;

    int getIndex(pid_t pid) const;

    void getInserted(std::vector<Location> *vec) const;

    void getRemoved(std::vector<Location> *vec) const;

    bool info(pid_t pid, TaskInfo *info) const;

private:

    TaskTree_Private * m_data;
};

#endif // TASKTREE_H
